shell script|November 24, 2017|1 min read

Shell script to extract single file from a bunch of jar files

TL;DR

Use a shell script with unzip command and a for loop to extract a specific file from each jar in a directory, organizing output into per-jar folders.

Shell script to extract single file from a bunch of jar files

Problem Statement

I have a bunch of jar files lying in a folder. Lets name it /Users/labuser/jars

Each jar file has a manifest file in META_INF/MANIFEST.MF
I have to read each MANIFEST.MF file and get some information from it. So, instead of extracting whole jar, one by one is a tedius thing.

What I want

I want to extract that file in a folder whose name can be as of jar file.

Unzip command to extract a jar file

Following command can be used to unzip a complete jar file:

unzip <path of jar file>

If you want to unzip files to a particular folder

unzip <path of jar file> -d <path of target folder>

Note: if the target folder does not exists, it will be created

If you want to unzip only particular files

unzip <path of jar file> <path of file inside jar file>

If you want to unzip only particular files in a particular folder

unzip <path of jar file> <path of file inside jar file> -d <target folder>

Conclusion

That is all. Combine these, and you are done.

Shell script I used to solve this

for myfile in `ls /Users/labuser/jars` ; do unzip ../$myfile META-INF/MANIFEST.MF -d $myfile; done

Related Posts

A Practical Guide in understanding Git Branch and Conflict resolution during merge

A Practical Guide in understanding Git Branch and Conflict resolution during merge

Introduction In this guide, We will learn about branching, handling conflict…

How to renew SSL certificate from Lets-encrypt when your website is using cloudflare

How to renew SSL certificate from Lets-encrypt when your website is using cloudflare

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

Python Code - How To Read CSV with Headers into an Array of Dictionary

Python Code - How To Read CSV with Headers into an Array of Dictionary

Introduction Lets assume we have a csv something similar to following: Python…

Eclipse/STS IDE showing compilation errors in java code inspite of all dependencies installed

Eclipse/STS IDE showing compilation errors in java code inspite of all dependencies installed

I have a Java project and dependencies are being managed through maven. I have…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Latest Posts

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

Serverless vs Containers — The Decision I Keep Revisiting

Serverless vs Containers — The Decision I Keep Revisiting

Every time I start a new service, I have the same argument with myself. Lambda…

System Design Patterns for Scaling Reads

System Design Patterns for Scaling Reads

Most production systems are read-heavy. A typical web application sees 90-9…

Building a Production RAG Pipeline — From Chunking to Retrieval to Generation

Building a Production RAG Pipeline — From Chunking to Retrieval to Generation

Large Language Models are powerful, but they hallucinate. They confidently make…

Prompt Engineering Patterns That Actually Work in Production

Prompt Engineering Patterns That Actually Work in Production

Most prompt engineering advice on the internet is useless in production. “Be…

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…